



setInterval(() => {
    location.reload();
}, 600000);


//https://www.ozon.ru/api/entrypoint-api.bx/page/json/v2?url=/product/1185794392/?layout_container=pdpPage2column&layout_page_index=2
function getQueryString(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    let r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return r[2];
    };
    return 0;
 }




console.log("shein ajax");


chrome.runtime.onMessage.addListener(async (msg, sender) => {
    console.log(msg);
    getPageByFetch(msg);
})



function  getPageByFetch(msg){

// 定义要请求的 URL
    const url = msg.data.url;

// 使用 fetch 发送 GET 请求
    fetch(url, {
        method: 'GET',
        credentials: 'include'  // 允许发送当前域名的 Cookie
    })
        .then(response => {
            if (!response.ok) {
                throw new Error('Network response was not ok ' + response.statusText);
            }
            return response.text();  // 将响应体转换为文本
        })
        .then(data => {
            console.log('Response data:', data);

            const regex = /window\.gbRawData\s*=\s*(.*?)(\n|\r\n)/;  // 匹配结尾为 ; 或换行符
            const match = data.match(regex);

            if (match && match[1]) {
                const gbRawData = match[1].trim();  // 提取到的 xxxx 部分并去掉多余空格
                console.log('Extracted gbRawData:', gbRawData);

                try {
                    // 将 gbRawData 转换为 JS 对象
                    const jsObject = JSON.parse(gbRawData);
                    console.log('Converted JavaScript Object:', jsObject);

                    getDataDetailPageV2(jsObject,data.id);

                    //getDataDetail(jsObject,msg.data.id);
                } catch (error) {
                    console.error('Failed to parse gbRawData as JSON:', error);
                }
            } else {

                if(data.includes("var GB_RISK_CHALLEGE_LANG")){

                    localStorage.clear();
                    sessionStorage.clear();

                    var type = "sheinblock";
                    console.log('sheinblock send.');
                    chrome.runtime.sendMessage({ data: {type }, close: false })

                    setTimeout(() => {
                        location.reload()
                    }, 10000);
                }
                console.log('No matching data found.');
            }

        })
        .catch((error) => {
            console.error('There was a problem with the fetch operation:', error);
        });

}

